
package com.jf.cloud.product.controller.supplier;

import com.jf.cloud.api.auth.constant.SysTypeEnum;
import com.jf.cloud.api.product.dto.CategoryShopDTO;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.i18n.I18nMessage;
import com.jf.cloud.common.product.vo.CategoryVO;
import com.jf.cloud.common.product.vo.app.CategoryAppVO;
import com.jf.cloud.common.response.ResponseEnum;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.product.model.CategoryShop;
import com.jf.cloud.product.service.CategoryService;
import com.jf.cloud.product.service.CategoryShopService;
import com.jf.cloud.product.vo.CategoryShopVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zz
 */
@RestController("supplierCategoryController")
@RequestMapping("/s/apply_supplier/category")
@Tag(name = "supplier-分类信息")
public class CategoryController {

    @Autowired
    private CategoryShopService categoryShopService;

    @Autowired
    private CategoryService categoryService;

    @PostMapping("/signing")
    @Operation(summary = "签约分类" , description = "签约分类")
    public ServerResponseEntity<Void> signingCategory(@Valid @RequestBody List<CategoryShopDTO> categoryShopDTOList) {
        Long supplierId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        categoryShopService.signingCategory(categoryShopDTOList, supplierId, sysType);
        return ServerResponseEntity.success();
    }

    @GetMapping("/list_by_supplier")
    @Operation(summary = "获取签约的分类列表(返回所有)" , description = "获取签约的分类列表(返回所有)")
    public ServerResponseEntity<List<CategoryShopVO>> listByShopId() {
        Long supplierId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        List<CategoryShopVO> categoryShopVOList = categoryShopService.listByShopId(supplierId, sysType, I18nMessage.getLang());
        return ServerResponseEntity.success(categoryShopVOList);
    }

    @GetMapping("/platform_categories")
    @Operation(summary = "获取平台所有的分类信息" , description = "获取所有的分类列表信息")
    public ServerResponseEntity<List<CategoryAppVO>> platformCategories() {
        return ServerResponseEntity.success(categoryService.platformCategories());
    }

    @GetMapping("/list_signing_category")
    @Operation(summary = "获取签约的可用分类列表(发布商品时使用,包含对应的上级分类）" , description = "获取签约的可用分类列表(发布商品时使用,包含对应的上级分类）")
    public ServerResponseEntity<List<CategoryAppVO>> listSigningCategory(@RequestParam(value = "status", required = false) Integer status) {
        List<CategoryAppVO> categoryList = categoryService.listSigningCategory(AuthUserContext.get().getTenantId(), AuthUserContext.get().getSysType(), I18nMessage.getLang());
        if (Objects.nonNull(status)) {
            categoryList = categoryList.stream().filter(categoryAppVO -> Objects.equals(categoryAppVO.getStatus(), status)).collect(Collectors.toList());
        }
        return ServerResponseEntity.success(categoryList);
    }

    @DeleteMapping("/delete_signing_category")
    @Operation(summary = "根据签约分类id删除签约信息" , description = "根据签约分类id删除签约信息")
    public ServerResponseEntity<Void> deleteSigningCategory(@RequestParam("categoryShopId") Long categoryShopId) {
        Long shopId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        CategoryShop categoryShop = categoryShopService.getById(categoryShopId);
        if (!Objects.equals(shopId, categoryShop.getShopId()) && !Objects.equals(sysType, SysTypeEnum.SUPPLIER.value())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        categoryShopService.deleteSigningCategory(categoryShop);
        return ServerResponseEntity.success();
    }

    @GetMapping("/list_apply_signing_category")
    @Operation(summary = "获取可以签约的平台分类列表（已经签约的平台分类不会返回）" , description = "获取可以签约的平台分类列表（已经签约的平台分类不会返回）")
    public ServerResponseEntity<List<CategoryVO>> listApplySigningCategory() {
        Long supplierId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        return ServerResponseEntity.success(categoryService.listApplySigningCategory(supplierId, sysType));
    }

    @PostMapping("/add_signing_category")
    @Operation(summary = "新增签约分类" , description = "新增签约分类")
    public ServerResponseEntity<Void> applyCategory(@Valid @RequestBody List<CategoryShopDTO> categoryShopDTOList) {
        Long shopId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        categoryShopService.addSigningCategory(categoryShopDTOList, shopId, sysType);
        return ServerResponseEntity.success();
    }

}
